package com.web;

import com.domain.User;
import com.service.userService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 员工修改客户的数据作用，现在查询员工个人数据
 * 接收 userShow.jsp 页面的 id 数据
 * 提交到 userService 的 findUserId 函数
 * 转发到 /update.jsp 页面进行展示
 */
@WebServlet("/findUserId")
public class findUserIdServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        //1.接受有传入的参数
        String id = request.getParameter("id");
        System.out.println("id = " + id);
        //2.调用service层的,根据id查询
        User user = new userService().findUserId(Integer.parseInt(id));
        System.out.println("查询了 user = " + user +" 的用户");
        //3.判断对象存不存在
        if(user != null){
            //4.1对象存在展示
            request.setAttribute("user", user);
            //4.2转发到update.jsp进行展示
            request.getRequestDispatcher("/update.jsp").forward(request,response);
        }else{
            //5.对象不存在,重定向到查询所有
            response.sendRedirect(request.getContextPath()+"/showUser");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
